﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BLL;

namespace IAATO
{
    public partial class ActiviteManagement : System.Web.UI.Page
    {
        ActiviteService activiteService;

        protected void Page_Load(object sender, EventArgs e)
        {
            activiteService = new ActiviteService(new ActiviteManager(), new SiteManager());

            #region binding gridView

            List<ACTIVITE> activites = activiteService.getAllActivites();
            if (activites == null)
            {
                lblError.Text = "An error has occurred";
            }
            else if (activites.Count == 0)
            {
                lblError.Text = "No activity in the database";
                gdwActivites.DataBind();
            }

            else
            {
                lblError.Text = "";
                gdwActivites.DataSource = activites;
                gdwActivites.DataBind();
            }

            #endregion
        }

        protected void btnAjouter_Click(object sender, EventArgs e)
        {
            activiteService.createActivite(txtNom.Text);
            Response.Redirect("ActiviteManagement.aspx");
        }

        protected void btnModifier_Click(object sender, EventArgs e)
        {
            activiteService.updateActivite(int.Parse(idUpdate.Value), txtNom.Text);

            btnModifier.Visible = false;
            btnAnnuler.Visible = false;
            btnAjouter.Visible = true;

            Response.Redirect("ActiviteManagement.aspx");
        }

        protected void btnAnnuler_Click(object sender, EventArgs e)
        {
            btnModifier.Visible = false;
            btnAnnuler.Visible = false;
            btnAjouter.Visible = true;
            txtNom.Text = "";
        }

        protected void delete_Click(object sender, EventArgs e)
        {
            activiteService.deleteActivite(int.Parse(((ImageButton)sender).CommandArgument));
            Response.Redirect("ActiviteManagement.aspx");
        }

        protected void update_Click(object sender, EventArgs e)
        {
            ACTIVITE activite = activiteService.getActiviteById(int.Parse(((ImageButton)sender).CommandArgument));

            txtNom.Text = activite.NOM;
            btnAjouter.Visible = false;
            btnModifier.Visible = true;
            btnAnnuler.Visible = true;

            idUpdate.Value = "" + activite.ID;

            hdfScrollIntoView.Value = "True";
        }

        protected void gdwActivites_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gdwActivites.DataSource = activiteService.getAllActivites();
            gdwActivites.PageIndex = e.NewPageIndex;
            gdwActivites.DataBind();
        }
    }
}